const express = require('express');
const ejs = require('ejs');
const app = express();
const userModel = require('./db/model/userModel');

// 配置获取请求体数据中间件
app.use(express.urlencoded({extended:false}));

// 配置ejs
app.set('views',__dirname+'/html');
app.engine('html',ejs.renderFile);

app.get('/',(req,res)=>{
    res.render('insert.html',{});
});

// 接收数据
app.post('/insert',(req,res)=>{
    console.log(req.body); //{ username: 'laowang', age: '18' }
    
    // 7.插入数据
    userModel.create({
        name:req.body.username,
        age:req.body.age
    },(err,data)=>{
        if(err) throw err;
        console.log(data);

        res.send('插入成功');
    });
})

app.listen(80,()=>{
    console.log('启动成功');
})